package state

import "fmt"

type IOrderState interface {
	GetState() string
	Create(m *OrderStateMachine)
	Audit(m *OrderStateMachine)
	Delivery(m *OrderStateMachine)
	CancelAudit(m *OrderStateMachine)
	CancelDelivery(m *OrderStateMachine)
	Cancel(m *OrderStateMachine)
}

var _ IOrderState = &CreatedState{}

type CancelState struct {
	*BaseState
}

type BaseState struct {
	state string
}

func (s *BaseState) GetState() string {
	return s.state
}
func (s *BaseState) Create(m *OrderStateMachine) {
	fmt.Println("不能操作")
}
func (s *BaseState) CancelAudit(m *OrderStateMachine) {
	fmt.Println("不能操作")
}
func (s *BaseState) CancelDelivery(m *OrderStateMachine) {
	fmt.Println("不能操作")
}
func (s *BaseState) Audit(m *OrderStateMachine) {
	fmt.Println("不能操作")
}
func (s *BaseState) Delivery(m *OrderStateMachine) {
	fmt.Println("不能操作")
}
func (s *BaseState) Cancel(m *OrderStateMachine) {
	fmt.Println("不能操作")
}

type InitState struct {
	*BaseState
}

func (s *InitState) Create(m *OrderStateMachine) {
	fmt.Println("创建成功")
	m.orderState = &CreatedState{&BaseState{state: "已创建"}}
}

// 创建状态
type CreatedState struct {
	*BaseState
}

func (s *CreatedState) Audit(m *OrderStateMachine) {
	fmt.Println("审核成功")
	m.orderState = &AuditedState{&BaseState{state: "已审核"}}
}
func (s *CreatedState) Cancel(m *OrderStateMachine) {
	fmt.Println("取消成功")
	m.orderState = &CancelState{&BaseState{state: "已取消"}}
}

type AuditedState struct {
	*BaseState
}

func (s *AuditedState) CancelAudit(m *OrderStateMachine) {
	fmt.Println("取消审核成功")
	m.orderState = &CreatedState{&BaseState{state: "已创建"}}
}

func (s *AuditedState) Delivery(m *OrderStateMachine) {
	fmt.Println("发货成功")
	m.orderState = &DeliveryState{&BaseState{state: "已发货"}}
}

type DeliveryState struct {
	*BaseState
}

func (s *DeliveryState) CancelDelivery(m *OrderStateMachine) {
	fmt.Println("取消发货")
	m.orderState = &AuditedState{&BaseState{state: "已审核"}}
}

type OrderStateMachine struct {
	orderState IOrderState
}

func NewOrderStateMachine() *OrderStateMachine {
	return &OrderStateMachine{
		orderState: &InitState{&BaseState{state: "初始化"}},
	}
}

func (m *OrderStateMachine) GetState() string {
	return m.orderState.GetState()
}

func (m *OrderStateMachine) Create() {
	m.orderState.Create(m)
}

func (m *OrderStateMachine) Audit() {
	m.orderState.Audit(m)
}

func (m *OrderStateMachine) Delivery() {
	m.orderState.Delivery(m)
}

func (m *OrderStateMachine) Cancel() {
	m.orderState.Cancel(m)
}

func (m *OrderStateMachine) CancelAudit() {
	m.orderState.CancelAudit(m)
}
func (m *OrderStateMachine) CancelDelivery() {
	m.orderState.CancelDelivery(m)
}
